
/**
  ******************************************************************************
  * Copyright 2021 The Microbee Authors. All Rights Reserved.
  * 
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
  * You may obtain a copy of the License at
  * 
  * http://www.apache.org/licenses/LICENSE-2.0
  * 
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  * 
  * @file       surface_tracking.h
  * @author     baiyang
  * @date       2023-2-11
  ******************************************************************************
  */

#pragma once

#ifdef __cplusplus
extern "C"{
#endif

/*----------------------------------include-----------------------------------*/
#include <stdint.h>
#include <stdbool.h>
/*-----------------------------------macro------------------------------------*/

/*----------------------------------typedef-----------------------------------*/
// surface tracking surface
enum SurfaceType {
    SURFACE_TYPE_NONE = 0,
    SURFACE_TYPE_GROUND = 1,
    SURFACE_TYPE_CEILING = 2
};

typedef struct surf_tracking* surface_tracking_t;
typedef const struct surf_tracking* surface_tracking_const_t;

struct surf_tracking {
    uint32_t last_update_ms;    // system time of last update to target_alt_cm
    uint32_t last_glitch_cleared_ms;    // system time of last handle glitch recovery
    enum SurfaceType surface;
    bool valid_for_logging;     // true if we have a desired target altitude
    bool reset_target;          // true if target should be reset because of change in surface being tracked
};
/*----------------------------------variable----------------------------------*/

/*-------------------------------------os-------------------------------------*/

/*----------------------------------function----------------------------------*/
// update_surface_offset - manages the vertical offset of the position controller to follow the measured ground or ceiling
//   level measured using the range finder.
void surftrack_update_surface_offset(surface_tracking_t surftrack);

// get target altitude (in cm) above ground
// returns true if there is a valid target
bool surftrack_get_target_alt_cm(surface_tracking_const_t surftrack, float *target_alt_cm);

// set target altitude (in cm) above ground
void surftrack_set_target_alt_cm(surface_tracking_t surftrack, float _target_alt_cm);

bool surftrack_get_target_dist_for_logging(surface_tracking_const_t surftrack, float *target_dist);
float surftrack_get_dist_for_logging(surface_tracking_const_t surftrack);

// set direction
void surftrack_set_surface(surface_tracking_t surftrack, enum SurfaceType new_surface);

// initialise surface tracking
static inline void surftrack_init(surface_tracking_t surftrack, enum SurfaceType surf) { surftrack->surface = surf; }
static inline void surftrack_invalidate_for_logging(surface_tracking_t surftrack) { surftrack->valid_for_logging = false; }
/*------------------------------------test------------------------------------*/

#ifdef __cplusplus
}
#endif



